package com.greateamd.timetableasy.client.widgets.user;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.greateamd.timetableasy.client.services.user.UserService;
import com.greateamd.timetableasy.client.services.user.UserServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;
import com.greateamd.timetableasy.client.widgets.ListBox;
import com.greateamd.timetableasy.domain.User;

public class CampusManagerListBox extends ListBox 
{
	private List<User> managers;
	
	public @UiConstructor CampusManagerListBox(boolean autoLoad) {
		if(autoLoad){
			loadCampusManagers();
		}
	}	

	public void loadCampusManagers() {
		final UserServiceAsync userService = GWT.create(UserService.class);
		new ServerCall<List<User>>() {

			@Override
			protected void callService(AsyncCallback<List<User>> asyncCallback) {
				userService.getAllCampusManager(asyncCallback);
			}

			@Override
			public void onSuccess(List<User> users) {
				managers = users;
				showListManager();
			}
		}.call();
	}

	protected void showListManager() {
		clear();
		for(User user : managers){
			addItem(user.getUsername());
		}
	}
	
	public User getSelectedUser(){
		return managers.get(getSelectedIndex());
	}
}
